<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\controller\admin\v1\system\config;

use app\controller\admin\Auth;
use app\Request;
use app\services\system\config\SystemConfigServices;
use think\facade\App;

/**
 * Class：系统配置控制器
 * 创建人：Jack
 * 创建时间：2023/11/24 17:27
 * @package app\controller\admin\v1\system\config
 */
class SystemConfig extends Auth
{
    /**
     * 构造方法
     * SystemConfig constructor.
     * @param App                  $app
     * @param SystemConfigServices $services
     */
    public function __construct(App $app, SystemConfigServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 注释：配置列表
     * 创建人：Jack
     * 创建时间：2023/8/24 10:23
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
            ['tab_id', 0],
            ['status', -1]
        ]);
        if (!$where['tab_id']) {
            return $this->fail('参数错误');
        }
        if ($where['status'] == -1) {
            unset($where['status']);
        }

        return $this->success($this->services->getConfigList($where));
    }

    /**
     * 注释：创建配置
     * 创建人：Jack
     * 创建时间：2023/8/24 10:49
     * @return mixed
     */
    public function create()
    {
        $data = $this->request->postMore([
            ['config_tab_id', []],
            ['title', ''],
            ['menu_name', ''],
            ['value', ''],
            ['remarks', ''],
            ['type', 0],
            ['sort', 0],
            ['status', 1]
        ]);
        if ($data['config_tab_id']) $data['config_tab_id'] = end($data['config_tab_id']);
        if (!$data['menu_name']) return $this->fail('请输入字段名称');
        if ($data['sort'] < 0) {
            $data['sort'] = 0;
        }
        $data['value'] = json_encode($data['value']);
        $config = $this->services->getOne(['menu_name' => $data['menu_name']]);
        if ($config) {
            $this->services->update($config['id'], $data, 'id');
        } else {
            $this->services->save($data);
        }
        event('config.create', [$data]);
        return $this->success('添加配置成功!');
    }

    /**
     * 注释：更新配置
     * 创建人：Jack
     * 创建时间：2023/8/30 16:08
     * @param $id
     * @return mixed
     */
    public function update($id)
    {
        $data = $this->request->postMore([
            ['config_tab_id', []],
            ['title', ''],
            ['menu_name', ''],
            ['value', ''],
            ['remarks', ''],
            ['type', 0],
            ['sort', 0],
            ['status', 1]
        ]);
        $data['config_tab_id'] = end($data['config_tab_id']);
        if (!$this->services->get($id)) {
            return $this->fail('编辑的记录不存在!');
        }
        $data['value'] = json_encode($data['value']);
        $this->services->update($id, $data);
        \hdphp\services\SystemConfigService::clear();

        return $this->success('修改成功!');
    }

    /**
     * 注释：删除配置
     * 创建人：Jack
     * 创建时间：2023/8/30 16:51
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$this->services->delete($id))
            return $this->fail('删除失败,请稍候再试!');
        else {
            event('config.delete', [$id]);
            return $this->success('删除成功!');
        }
    }

    /**
     * 注释：修改配置状态
     * 创建人：Jack
     * 创建时间：2023/8/30 16:53
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if ($status == '' || $id == 0) {
            return $this->fail('参数错误');
        }
        $this->services->update($id, ['status' => $status]);
        event('config.status', [$id, $status]);

        return $this->success($status == 0 ? '隐藏成功' : '显示成功');
    }


    /**
     * 注释：获取单个配置的值
     * 创建人：Jack
     * 创建时间：2023/9/1 14:27
     * @param $name
     * @return mixed
     */
    public function get_system($name)
    {
        $value = sys_config($name);
        return $this->success(compact('value'));
    }

    /**
     * 注释：获取某个分类下的所有配置
     * 创建人：Jack
     * 创建时间：2023/9/1 14:39
     * @param $tabId
     * @return mixed
     */
    public function get_config_list($tabId)
    {
        $list = $this->services->getConfigTabAllList($tabId);
        $data = [];
        foreach ($list as $item) {
            $data[$item['menu_name']] = json_decode($item['value']);
        }
        return app('json')->success($data);
    }

    /**
     * 注释：保存数据
     * 创建人：Jack
     * 创建时间：2023/9/2 15:12
     * @param Request $request
     * @return mixed
     */
    public function save_basics(Request $request)
    {
        $post = $this->request->post();
        foreach ($post as $k => $v) {
            $config_one = $this->services->getOne(['menu_name' => $k]);
            if ($config_one) {
                $config_one['value'] = $v;
                $val = is_null($v) ? '' : json_encode($v);
                $this->services->update($k, ['value' => $val], 'menu_name');
            }
        }
        \hdphp\services\SystemConfigService::clear();
        return $this->success('修改成功');
    }

    /**
     * 注释：获取缩略图配置
     * 创建人：Jack
     * 创建时间：2023/11/30 15:51
     * @return mixed
     */
    public function getImageConfig()
    {
        return $this->success($this->services->getImageConfig());
    }

    
}